package com.project.psc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.dto.SupplierVerifyResultDto;
import com.project.psc.bean.CheckBean;
import com.project.psc.bean.SupplierBean;
import com.project.psc.dto.SupplierDto;
import com.project.psc.mapper.ICheckMapper;
import com.project.psc.mapper.ISupplierMapper;
import com.project.psc.service.ISupplierService;
import com.project.util.ConvertBeanUtil;
import com.project.util.MD5Encryptor;
import lombok.SneakyThrows;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SupplierServiceImpl implements ISupplierService {
    @Autowired
    private ISupplierMapper supplierMapper;

    @Autowired
    private ICheckMapper checkMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Override
    public SupplierBean findById(Long id) {
        SupplierBean supplierBean = supplierMapper.selectById(id);
        supplierBean.setPassword("");
        return supplierBean;
    }

    @SneakyThrows
    @Override
    public boolean register(SupplierBean supplier) {
        //密码加密
        String encrypt = MD5Encryptor.encrypt(supplier.getPassword());
        supplier.setPassword(encrypt);
        //查询用户名是否重名
        QueryWrapper<SupplierBean> qw = new QueryWrapper<>();
        qw.eq("s_userName", supplier.getUserName());
        Long count = supplierMapper.selectCount(qw);
        //审核不通过，重新修改注册消息注册的
        if (supplier.getId() != null) {
            SupplierBean supplierBean = supplierMapper.selectById(supplier.getId());
            if (count == 0 || supplierBean.getUserName().equals(supplier.getUserName())) {
                supplier.setStatus(0);
                supplierMapper.updateById(supplier);
                //转换为json格式
                String json = JSON.toJSONString(supplier);
                //通过MQ发送给审核
                rabbitTemplate.convertAndSend("supplierOne", "supplier.register", json);
                return true;
            }
        }
        //新供应商注册
        if (count == 0) {//没找到可以添加用户
            supplierMapper.insert(supplier);
            //转换为json格式
            String json = JSON.toJSONString(supplier);
            //通过MQ发送给审核
            rabbitTemplate.convertAndSend("supplierOne", "supplier.register", json);
            return true;
        }

        return false;
    }

    @Override
    @RabbitListener(queues = "SupplierVerifyResult")
    public void addCheck(String json) {
        SupplierVerifyResultDto resultDto = JSONObject.parseObject(json, SupplierVerifyResultDto.class);
        SupplierBean supplier = supplierMapper.selectById(resultDto.getSupplierRef());
        supplier.setStatus(resultDto.getStatus());
        supplierMapper.updateById(supplier);
        if (resultDto.getStatus() == 2) {
            CheckBean check = new CheckBean();
            check.setSupplierId(resultDto.getSupplierRef());
            check.setReason(resultDto.getCause());
            checkMapper.insert(check);
        }
    }

    @Override
    @SneakyThrows
    public SupplierDto login(String userName, String password) {

        QueryWrapper<SupplierBean> qw = new QueryWrapper<>();
        qw.eq("s_userName", userName);
        qw.eq("s_password", MD5Encryptor.encrypt(password));
        SupplierBean supplierBean = supplierMapper.selectOne(qw);
        if (supplierBean != null) {//找到了
            return ConvertBeanUtil.copy(supplierBean, SupplierDto.class);
        }
        return null;
    }

    @Override
    public List<CheckBean> findReasons(Long supplierId) {
        return supplierMapper.findErrs(supplierId);
    }
}
